# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=openjdk-mandrel
pkgver=23.0.0.0
pkgrel=0
_jdkver=17
pkgdesc="GraalVM Native Image builder for OpenJDK $_jdkver"
url="https://github.com/graalvm/mandrel"
# Upstream supports only these architectures.
# aarch64: openjdk exception when building
arch="x86_64"
license="GPL-2.0-only WITH Classpath-exception-2.0"
depends="
	bash
	gcc
	musl-dev
	openjdk$_jdkver
	zlib-dev
	zlib-static
	"
makedepends="
	git
	ninja-build
	ninja-is-really-ninja
	openjdk$_jdkver-static-libs
	python3
	py3-setuptools
	"
provides="openjdk$_jdkver-mandrel"
_mx_ver=6.27.3
source="mandrel-packaging-$pkgver-Final.tar.gz::https://github.com/graalvm/mandrel-packaging/archive/mandrel-$pkgver-Final.tar.gz
	https://github.com/graalvm/mandrel/archive/mandrel-$pkgver-Final.tar.gz
	https://github.com/graalvm/mx/archive/$_mx_ver/graalvm-mx-$_mx_ver.tar.gz
	fix-file-system-loop-exception.patch
	jdk-17.0.10-compatibility_patch
	Example.java
	"
builddir="$srcdir/mandrel-packaging-$pkgver"
# net: fetches some dependencies in build phase
# textrels: libnative-image-*.so have TEXTRELs.
options="net textrels"

export MX_CACHE_DIR="$srcdir/.mx"
export JAVA_HOME="/usr/lib/jvm/java-$_jdkver-openjdk"

unpack() {
	default_unpack

	mv mandrel-mandrel-$pkgver-Final mandrel-$pkgver
	mv mandrel-packaging-mandrel-$pkgver-Final mandrel-packaging-$pkgver
}

prepare() {
	default_prepare

	cd "$srcdir"/mandrel-$pkgver
	msg "jdk-17.0.10-compatibility_patch"
	patch -p1 -i "$srcdir"/jdk-17.0.10-compatibility_patch

	cd "$srcdir"
	# Build scripts require the sources to be git repositories.
	local dir; for dir in mandrel-$pkgver mandrel-packaging-$pkgver; do
		cd "$srcdir/$dir"
		git init --quiet
		git add .
		git -c user.name=mandrel -c user.email=mandrel@example.com commit --quiet --message Import
		git tag "mandrel-$pkgver-Final"
	done

	mkdir -p "$srcdir/.home"
}

build() {
	java -ea build.java \
		--mx-home "$srcdir/mx-$_mx_ver" \
		--mandrel-home "$builddir/dist" \
		--mandrel-repo "$srcdir/mandrel-$pkgver" \
		--mandrel-version "$pkgver" \
		--skip-clean
}

# TODO: Run upstream's integration tests.
check() {
	mkdir -p "$srcdir/test"
	cd "$srcdir"/test

	cp ../Example.java .
	javac Example.java
	GRAALVM_HOME="$builddir/dist" "$builddir"/dist/bin/native-image Example

	ldd example
	ls -lh example
	./example
}

package() {
	local destdir="$pkgdir$JAVA_HOME"

	cd "$builddir"/dist

	# Copy only files unique to Mandrel (the rest is copied from OpenJDK).
	mkdir -p "$destdir"/lib
	cp -r lib/libnative-image-*.so \
		lib/graalvm \
		lib/jvmci \
		lib/svm \
		lib/truffle \
		"$destdir"/lib/

	mkdir -p "$destdir"/bin
	cp -d bin/native-image "$destdir"/bin/

	# Remove source JARs.
	find "$destdir" -name '*.zip' -delete
}

sha512sums="
537afba9564e3dfeb7fe44c046f9007a12a01101b1defa8f49816d83eb7406276985ddd656b529678da1ed3bb8f3bd9ad35674593c058c6a88c0bf55a3d12b69  mandrel-packaging-23.0.0.0-Final.tar.gz
35fb7a96cd08cc7f94222831a95ea2f66158e128294ff44b9d686c2a8344a1fdfa42f627ef4d3913f71f768dbe9761e5d03923c61d15832198d81b3db91f84dc  mandrel-23.0.0.0-Final.tar.gz
5e89961a80855ae46973fb6d995320b3134d9bf71442949147bef23a9a15453cb978f753f64ccfc87023059654bad1330ab25483bd6c022f520b7f6e50ddc1fc  graalvm-mx-6.27.3.tar.gz
8d98ef2316a12e58e2a7d920533fcaab30dc81ceadba66206af1af9a8f7f21af5cac41a6a80d2a4aef8c1eb3b293a518f242ec0530e5c955418bfb7da1d4f72b  fix-file-system-loop-exception.patch
fc4c316b1e1a98aa313f8709f9430f3179f7efd7a0edcccd6b3dcb20cb3b4e84dd1cbfce4bdbd3d8221a7943bfa6df1e891d8198e922c9691f471b188114df11  jdk-17.0.10-compatibility_patch
601ac59e59d32cbaf91f7541bd8173b38c68cbec96a7fb74227017afc8e5dc6ce4b8be03e92dd34cf52370e5d52beb780808af57b17c79c53ae8ec64cd34a92d  Example.java
"
